#pragma once
#include "../common/net.h"
#include "../common/message.h"
#include <future>
#include <functional>

namespace rpc
{
    namespace client
    {
        class Requestor
        {
        public:
            using ptr = std::shared_ptr<Requestor>;
            using RequestCallBack = std::function<void(const BaseMessage::ptr &)>;
            using AsyncResponse = std::future<BaseMessage::ptr>;
            struct RequestorDescribe
            {
                using ptr = std::shared_ptr<RequestorDescribe>;
                BaseMessage::ptr request;
                RType rtype;
                std::promise<BaseMessage::ptr> response;
                RequestCallBack callback;
            };
            void onResponse(const BaseConnection::ptr &conn, BaseMessage::ptr &msg)
            {
                std::string rid = msg->rid();
                RequestorDescribe::ptr rdp = getDescribe(rid);
                if (rdp.get() == nullptr)
                {
                    ELOG("收到响应 - %s,是未找到对应的请求描述!", rid.c_str());
                    return;
                }
                if (rdp->rtype == RType::REQ_ASYNC)
                {
                    rdp->response.set_value(msg);
                }
                else if (rdp->rtype == RType::REQ_CALLBACK)
                {
                    if (rdp->callback)
                    {
                        rdp->callback(msg);
                    }
                }
                else
                {
                    ELOG("未知类型");
                }
                Deldescribe(rid);
            }
            bool send(const BaseConnection::ptr &conn, const BaseMessage::ptr &req, AsyncResponse &async_rsp)
            {
                RequestorDescribe::ptr rdp = newDescribe(req, RType::REQ_ASYNC);
                if (rdp.get() == nullptr)
                {
                    ELOG("构造请求不存在");
                    return false;
                }
                conn->send(req);
                async_rsp = rdp->response.get_future();
                return true;
            }
            bool send(const BaseConnection::ptr &conn, const BaseMessage::ptr &req, BaseMessage::ptr &rsp)
            {
                AsyncResponse rsp_future;
                bool ret = send(conn, req, rsp_future);
                if (ret == false)
                {
                    ELOG("构造请求不存在");
                    return false;
                }
                rsp = rsp_future.get();
                return true;
            }
            bool send(const BaseConnection::ptr &conn, const BaseMessage::ptr &req, const RequestCallBack &cb)
            {
                RequestorDescribe::ptr rdp = newDescribe(req, RType::REQ_ASYNC, cb);
                if (rdp.get() == nullptr)
                {
                    ELOG("构造请求描述对象失败！");
                    return false;
                }
                conn->send(req);
                return true;
            }

        private:
            RequestorDescribe::ptr newDescribe(const BaseMessage::ptr &req, RType type, const RequestCallBack &cb = RequestCallBack())
            {
                std::unique_lock<std::mutex> lock(_mutex);
                RequestorDescribe::ptr rd = std::make_shared<RequestorDescribe>();
                rd->request = req;
                rd->rtype = type;
                if (type == RType::REQ_CALLBACK && cb)
                {
                    rd->callback = cb;
                }
                _request_desc.insert(std::make_pair(req->rid(), rd));
                return rd;
            }
            RequestorDescribe::ptr getDescribe(const std::string &rid)
            {
                std::unique_lock<std::mutex> lock(_mutex);
                auto it = _request_desc.find(rid);
                if (it == _request_desc.end())
                {
                    return RequestorDescribe::ptr();
                }
                return it->second;
            }
            void Deldescribe(const std::string &rid)
            {
                std::unique_lock<std::mutex> lock(_mutex);
                _request_desc.erase(rid);
            }

        private:
            std::mutex _mutex;
            std::unordered_map<std::string, RequestorDescribe::ptr> _request_desc;
        };
    }
}
